/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.check.pendingabnormal.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.check.pendingabnormal.entity.PendingAbnormal;
import com.jeeplus.modules.check.pendingabnormal.mapper.PendingAbnormalMapper;
import com.jeeplus.modules.check.pendingabnormal.entity.PendingAbnormalChild;
import com.jeeplus.modules.check.pendingabnormal.mapper.PendingAbnormalChildMapper;

/**
 * pendingabnormalService
 * @author 刘明星
 * @version 2020-11-21
 */
@Service
@Transactional(readOnly = true)
public class PendingAbnormalService extends CrudService<PendingAbnormalMapper, PendingAbnormal> {

	@Autowired
	private PendingAbnormalChildMapper pendingAbnormalChildMapper;

	public PendingAbnormal get(String id) {
		PendingAbnormal pendingAbnormal = super.get(id);
		pendingAbnormal.setPendingAbnormalChildList(pendingAbnormalChildMapper.findList(new PendingAbnormalChild(pendingAbnormal)));
		return pendingAbnormal;
	}

	public List<PendingAbnormal> findList(PendingAbnormal pendingAbnormal) {
		return super.findList(pendingAbnormal);
	}

	public Page<PendingAbnormal> findPage(Page<PendingAbnormal> page, PendingAbnormal pendingAbnormal) {
		return super.findPage(page, pendingAbnormal);
	}

	@Transactional(readOnly = false)
	public void save(PendingAbnormal pendingAbnormal) {
		super.save(pendingAbnormal);
		for (PendingAbnormalChild pendingAbnormalChild : pendingAbnormal.getPendingAbnormalChildList()){
			if (pendingAbnormalChild.getId() == null){
				continue;
			}
			if (PendingAbnormalChild.DEL_FLAG_NORMAL.equals(pendingAbnormalChild.getDelFlag())){
				if (StringUtils.isBlank(pendingAbnormalChild.getId())){
					pendingAbnormalChild.setOut(pendingAbnormal);
					pendingAbnormalChild.preInsert();
					pendingAbnormalChildMapper.insert(pendingAbnormalChild);
				}else{
					pendingAbnormalChild.preUpdate();
					pendingAbnormalChildMapper.update(pendingAbnormalChild);
				}
			}else{
				pendingAbnormalChildMapper.delete(pendingAbnormalChild);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(PendingAbnormal pendingAbnormal) {
		super.delete(pendingAbnormal);
		pendingAbnormalChildMapper.delete(new PendingAbnormalChild(pendingAbnormal));
	}

}